package com.app.catchgpscoord;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;
import android.widget.Toast;

public class GPS_handler extends Activity{
	private ProgressDialog pd;
	private TextView txtv_latitude;
	private TextView txtv_length;
	private Context ctx;
	
	
	LocationManager mLocationManager;
	Location mLocation;
	MyLocationListener mLocationListener;
	
	Location currentLocation = null;

    public GPS_handler(TextView txtv_latitude, TextView txtv_length, Context ctx, LocationManager mLocationManager) {
		super();
		this.txtv_latitude = txtv_latitude;
		this.txtv_length = txtv_length;
		this.ctx = ctx;
		pd = new ProgressDialog(this.ctx);
		this.mLocationManager = mLocationManager;
	}


	private void setCurrentLocation(Location loc) {
    	currentLocation = loc;
    }
    
    
    public void writeSignalGPS() {
    	final Context ctx_tmp = this.ctx;
    	DialogInterface.OnCancelListener dialogCancel = new DialogInterface.OnCancelListener() {

            public void onCancel(DialogInterface dialog) {
            	android.widget.Toast.makeText(ctx_tmp, ctx_tmp.getResources().getString(R.string.gps_signal_not_found), android.widget.Toast.LENGTH_LONG).show();
                handler.sendEmptyMessage(0);
            }
        };
    	
		pd = ProgressDialog.show(this.ctx, this.ctx.getResources().getString(R.string.search), 
				this.ctx.getResources().getString(R.string.search_signal_gps), true, true, dialogCancel);
		buscar();
		return;
    }


	public void buscar() {
		
		
		if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
			mLocationListener = new MyLocationListener(this.ctx);
			mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocationListener);
			
		} else {
			android.widget.Toast.makeText(this.ctx, 
                    this.ctx.getResources().getString(R.string.gps_signal_not_found), 
                    android.widget.Toast.LENGTH_LONG).show();
		}
		return;
	}
	
	private Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			pd.dismiss();
			mLocationManager.removeUpdates(mLocationListener);
	    	if (currentLocation!=null) {
	    		txtv_latitude.setText("" + currentLocation.getLatitude());
	    		txtv_length.setText("" + currentLocation.getLongitude());
	    	}
		}
	};
	
	private class MyLocationListener implements LocationListener 
	{
		private Context ctx;
		
		
	    public MyLocationListener(Context ctx) {
			super();
			this.ctx = ctx;
		}

		@Override
	    public void onLocationChanged(Location loc) {
	        if (loc != null) {
	            Toast.makeText(this.ctx, this.ctx.getResources().getString(R.string.gps_signal_found),Toast.LENGTH_LONG).show();
	            setCurrentLocation(loc);
	            handler.sendEmptyMessage(0);
	        }
	    }
	
	    @Override
	    public void onProviderDisabled(String provider) {
	        // TODO Auto-generated method stub
	}
	
	@Override
	public void onProviderEnabled(String provider) {
	    // TODO Auto-generated method stub
	}
	
	@Override
	public void onStatusChanged(String provider, int status, 
	    Bundle extras) {
	    // TODO Auto-generated method stub
	    }
	} 


}
